package com.twb.core.config;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class TwbBeanInitCostTimePostProcessor implements BeanPostProcessor {

	private static final Logger logger = LoggerFactory.getLogger(TwbBeanInitCostTimePostProcessor.class);
	
	private static final ConcurrentHashMap<String, Long> START_TIME = new ConcurrentHashMap<>();
	
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		START_TIME.put(beanName, System.currentTimeMillis());
		return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
	}
	
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(Objects.nonNull(START_TIME.get(beanName))) {
			Long costTime = System.currentTimeMillis()-START_TIME.get(beanName);
			if(costTime>100) {
				logger.error("beanName:{},cost:{}",beanName,costTime);
			}
		}
		return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
	}
}
